import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import App from './App.vue'
import router from './router'

// 导入性能监控工具
import { performanceMonitor } from './utils/performance'
// 导入预加载管理器
import { preloadManager } from './utils/preload'
// 导入字体加载优化工具
import { fontUtils } from './utils/fontLoader'
// 导入Service Worker管理器
import { serviceWorkerManager } from './utils/serviceWorker'

const app = createApp(App)

// 注册Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.use(createPinia())
app.use(router)
app.use(ElementPlus)

app.mount('#app')

// 注册Service Worker
if (import.meta.env.PROD) {
    serviceWorkerManager.register().then(success => {
        if (success) {
            console.log('🚀 Service Worker 已注册')
        } else {
            console.warn('⚠️ Service Worker 注册失败')
        }
    })
}

// 开发环境下的性能监控
if (import.meta.env.DEV) {
    console.log('🚀 性能监控已启用')
    window.addEventListener('load', () => {
        setTimeout(() => {
            performanceMonitor.analyzeCodeSplitting()
            performanceMonitor.checkPerformance()
        }, 1000)
    })
}

// 显示预加载统计
console.log('📊 预加载统计:', preloadManager.getPreloadStats())

// 显示字体加载状态
console.log('🔤 字体加载状态:', fontUtils.getStatus())

// 定期清理过期缓存
setInterval(() => {
    import('./utils/httpCache').then(({ httpCacheManager }) => {
        httpCacheManager.cleanup()
    })
}, 5 * 60 * 1000) // 每5分钟清理一次